//
//  XMNewFeatureController.m
//  XMWeibo
//
//  Created by chi on 14-8-21.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMNewFeatureController.h"
#import "XMTabBarController.h"

#define kImageCount 3


@interface XMNewFeatureController () <UIScrollViewDelegate>

//滚动视图
@property (nonatomic, weak) UIScrollView *scrollView;

//分页控件
@property (nonatomic, weak) UIPageControl *pageControl;

//分享给好友
@property (nonatomic, weak) UIButton *checkBox;

@end

@implementation XMNewFeatureController



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //创建scrollview
    [self setupScrollview];
   
    //添加图片
    [self addImages];
    
    //创建分页控件
    [self setupPageControl];
}


#pragma mark - 响应点击开始微博
- (void)startWeibo
{
    

    XMTabBarController *tab = [[XMTabBarController alloc]init];
    
    
    UIApplication *application = [UIApplication sharedApplication];
    application.statusBarHidden = NO;
    
    UIWindow *window = application.windows[0];
    
    window.rootViewController = tab;
}


#pragma mark - scrollview代理

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat curpage = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    int curPageInt = (int)(curpage + 0.5);
    
    self.pageControl.currentPage = curPageInt;
    
}


#pragma mark - 添加分页控件

- (void)setupPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    
    pageControl.numberOfPages = kImageCount;
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    pageControl.pageIndicatorTintColor = [UIColor grayColor];

    [self.view addSubview:pageControl];
    
    CGFloat viewW = self.view.bounds.size.width;
    CGFloat viewH = self.view.bounds.size.height;
    
    pageControl.center = CGPointMake(viewW * 0.5, viewH - 30.0);
    
    self.pageControl = pageControl;
    
}

#pragma mark -

 //添加图片
- (void)addImages
{
    CGFloat viewW = self.view.bounds.size.width;
    CGFloat viewH = self.view.bounds.size.height;
    for (int i = 0; i < kImageCount; ++i) {
        
        
        
        NSString *name;
        if ([UIScreen mainScreen].bounds.size.height == 568.0) {
            name = [NSString stringWithFormat:@"new_feature_%d-568h@2x", i + 1];
        }
        else {
            name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        }
        
        
        
        UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage loadImageWithBundle:nil ContentsOfFile:name ofType:@"png"]];
        imageview.frame = CGRectMake(viewW * i, 0.0, viewW, viewH);
        
        [self.scrollView addSubview:imageview];
        
        if (i == kImageCount - 1) {
            [imageview setUserInteractionEnabled:YES];
            [self setupStartWeiboButton:imageview];
            [self setupShareCheckBox:imageview];
        }
        
    }
}

#pragma mark - 创建checkbox
- (void)setupShareCheckBox:(UIView*)view
{
    CGFloat viewW = self.view.bounds.size.width;
    CGFloat viewH = self.view.bounds.size.height;

    
    UIButton *checkBox = [UIButton buttonWithType:UIButtonTypeCustom];
    [checkBox setImage:[UIImage loadImageWithBundle:nil ContentsOfFile:@"new_feature_share_false" ofType:@"png"] forState:UIControlStateNormal];
    [checkBox setImage:[UIImage loadImageWithBundle:nil ContentsOfFile:@"new_feature_share_true" ofType:@"png"] forState:UIControlStateSelected];
    [checkBox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [checkBox.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
    [checkBox setAdjustsImageWhenHighlighted:NO];
    [checkBox addTarget:self action:@selector(clickCheckBox:) forControlEvents:UIControlEventTouchUpInside];
    
    checkBox.selected = YES;
    
    checkBox.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 0.0);
    
    [checkBox setTitle:@"分享给好友" forState:UIControlStateNormal];
    
    CGFloat checkBoxH = 30.0;
    CGFloat checkBoxW = 200.0;
    
    checkBox.bounds = CGRectMake(0.0, 0.0, checkBoxW, checkBoxH);
    checkBox.center = CGPointMake(viewW * 0.5, viewH * 0.6 - checkBoxH - 10.0);
    
    self.checkBox = checkBox;
    
    [view addSubview:checkBox];
}

//响应分享checkbox
- (void)clickCheckBox:(UIButton*)button
{
    button.selected = !button.isSelected;
}


#pragma mark - 创建开始微博按钮
- (void)setupStartWeiboButton:(UIView*)view
{
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [startButton setBackgroundImage:[UIImage loadImageWithBundle:nil ContentsOfFile:@"new_feature_finish_button" ofType:@"png"] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage loadImageWithBundle:nil ContentsOfFile:@"new_feature_finish_button_highlighted" ofType:@"png"] forState:UIControlStateHighlighted];
    
    startButton.bounds = (CGRect){CGPointZero, startButton.currentBackgroundImage.size};
    
    CGFloat viewW = self.view.bounds.size.width;
    CGFloat viewH = self.view.bounds.size.height;
    
    startButton.center = CGPointMake(viewW * 0.5, viewH * 0.6);
    [startButton setTitle:@"开始微博" forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(startWeibo) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:startButton];
}

//创建scrollview
- (void)setupScrollview
{
    CGFloat viewW = self.view.bounds.size.width;
    
    UIScrollView *scrollview = [[UIScrollView alloc]init];
    scrollview.frame = self.view.bounds;
    scrollview.contentSize = CGSizeMake(viewW * kImageCount, 0.0);
    //使用分页效果
    scrollview.pagingEnabled = YES;
    //去除弹簧效果
    scrollview.bounces = NO;
    //去除滚动条
    scrollview.showsHorizontalScrollIndicator = NO;
    //设置代理
    scrollview.delegate = self;
    
    scrollview.userInteractionEnabled = YES;
    
    self.scrollView = scrollview;
    
    
    [self.view addSubview:scrollview];
}

@end
